package com.syp.test.clustercacheflush.builder;

import java.util.Calendar;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.syp.test.clustercacheflush.domain.SettingsEntity;
import com.syp.test.clustercacheflush.domain.TaskEntity;
import com.syp.test.clustercacheflush.service.TaskStatus;
import com.syp.test.clustercacheflush.service.TaskType;
import com.syp.test.clustercacheflush.util.zkflushcache.CacheTypeEnum;
import com.syp.test.clustercacheflush.util.zkflushcache.ZkCacheSyncNotify;

/**
 * 任务构建器
 * @author syp
 *
 */
@Component
public class TaskBuilder {

	/**
	 * 生成失败手动重发任务
	 * 
	 * @param setting
	 * @return
	 */
	public TaskEntity buildClusterCacheSyncNotifyTask(SettingsEntity setting) {
		TaskEntity task = new TaskEntity();

		ZkCacheSyncNotify zkNotify = new ZkCacheSyncNotify();
		zkNotify.setCacheType(CacheTypeEnum.SETTINGS);
		zkNotify.setCacheKey(setting.getKey());
		
		task.setTaskType(TaskType.CLUSTER_CACHE_SYNC_NOTIFY_TASK.getType());
		task.setBizId(setting.getKey());
		task.setTaskData(JSON.toJSONString(zkNotify));
		task.setTaskStatus(TaskStatus.LOCK.getStatus());
		task.setRetryCount(0);
		
		// 取5分钟后的时间
		task.setBizTime(Calendar.getInstance().getTime());
		task.setLastUpdate(Calendar.getInstance().getTime());
		task.setCreateTime(Calendar.getInstance().getTime());

		// task uuid
		StringBuilder sb = new StringBuilder();
		sb.append(task.getTaskType());
		sb.append("-").append(task.getBizId());
		sb.append("-").append(setting.getLastUpdate().getTime());

		task.setUuid(sb.toString());

		return task;
	}

}
